package com.lanou.service;

import com.lanou.entity.Prize;
import org.junit.Test;

import java.util.*;

public class PrizeService {
    public List<Prize> findAllPrize() {
        List<Prize> prizeList = new ArrayList<>();
        prizeList.add(new Prize(1, "奖项一", "", 0.01));
        prizeList.add(new Prize(2, "奖项二", "", 0.11));
        prizeList.add(new Prize(3, "奖项三", "", 0.22));
        prizeList.add(new Prize(4, "奖项四", "", 0.33));
        prizeList.add(new Prize(5, "奖项五", "", 0.33));
        return prizeList;
    }

    public Integer doLottery() {
        List<Prize> prizeList = findAllPrize();

        Random r = new Random();
        double v = r.nextDouble();
        double temp = 0D;
        for (Prize prize : prizeList) {
            temp += prize.getProbability();
            if (temp >= v) {
                return prize.getId();
            }
        }
        return null;
    }

    @Test
    public void test1() {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < 100000; i++) {
            Integer result = doLottery();
            if (map.containsKey(result)) {
                map.put(result, map.get(result) + 1);
            } else {
                map.put(result, 1);
            }
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

}
